﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace PathFinder.Sources
{

    public partial class ExtendedPanel : Panel
    {
        public Image background;
        public PictureBox drawablePicture = new PictureBox();

        public ExtendedPanel()
        {
            this.DoubleBuffered = true;
            drawablePicture.Top = 0;
            drawablePicture.Left = 0;
            Controls.Add(drawablePicture);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
        ControlStyles.UserPaint |
        ControlStyles.OptimizedDoubleBuffer, true);
            UpdateStyles();
            this.MinimumSize = new Size(400, 400);
        }

        public void AttachBackground(Image background)
        {
            this.drawablePicture.Dispose();
            this.drawablePicture = new PictureBox();
            drawablePicture.Top = 0;
            drawablePicture.Left = 0;
            Controls.Add(drawablePicture);
            this.background = background;
            ToGrayScale();
            drawablePicture.Image = this.background;
            this.drawablePicture.Size = this.background.Size;
            AssurePictureMinimumsize();
            this.MaximumSize = new Size(background.Width + 20, background.Height + 20);
        }

        private void AssurePictureMinimumsize()
        {
            Size s = new Size();
            if (background.Height < 400)
            {
                s.Height = 400;
            }
            else
            {
                s.Height = background.Height;
            }
            if (background.Width < 400)
            {
                s.Width = 400;
            }
            else
            {
                s.Width = background.Width;
            }
            Bitmap blank = new Bitmap(s.Width,s.Height);
            Graphics tempG = Graphics.FromImage(blank);
            tempG.FillRectangle(new SolidBrush(Color.White), 0, 0, s.Width, s.Height);
            tempG.DrawImage(background,Point.Empty);
            drawablePicture.Image = blank;
            background = blank;
            this.drawablePicture.Size = s;
        }

        public void AttachBackground(Size s)
        {
            this.drawablePicture.Dispose();
            this.drawablePicture = new PictureBox();
            drawablePicture.Top = 0;
            drawablePicture.Left = 0;
            Controls.Add(drawablePicture);
            Bitmap blank = new Bitmap(s.Width, s.Height);
            Graphics temporaryG = Graphics.FromImage(blank);
            temporaryG.FillRectangle(new SolidBrush(Color.White), 0, 0, s.Width, s.Height);
            this.background = blank;
            this.drawablePicture.Image = blank;
            this.drawablePicture.Size = s;
            this.MaximumSize = new Size(s.Width + System.Windows.Forms.SystemInformation.VerticalScrollBarWidth, s.Height + System.Windows.Forms.SystemInformation.HorizontalScrollBarHeight);
        }

        private void ToGrayScale()
        {
            Bitmap bmp = new Bitmap(background);
            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color pixelColor = bmp.GetPixel(x, y);
                    int grayScale = (int)((pixelColor.R * .3) + (pixelColor.G * .59) + (pixelColor.B * .11));
                    Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
                    bmp.SetPixel(x, y, newColor);
                }
            }
            background = (Image)bmp;
        }

    }
}
